<template>
    <!--        选中商品的弹框-->
    <div>
        <el-drawer
                title="我是标题"
                :visible.sync="isShow"
                :with-header="false"
                size='70%'
                @closed="handleClose"
        >
            <avue-crud :option="option"
                       :data="data"
                       ref="crud"
                       :table-loading="loading"
                       :page="page"
                       @selection-change="selectionChange"
                       @current-change="currentChange"
                       @size-change="sizeChange"
                       @on-load="onLoad">
                <template slot-scope="{row}"
                          slot="goodsMainPhotoUrl">
                    <img :src="row.goodsMainPhotoUrl"
                         style="width:50px;height:50px;"/>
                </template>
            </avue-crud>
            <div class="btn">
                <div class="">
                    <el-button @click="handleClose">取 消</el-button>
                    <el-button type="primary" @click="sureBtn">确定</el-button>
                </div>
            </div>
        </el-drawer>
    </div>
</template>
<script>
    import {
        getList,
    } from "@/api/base/goods/goods";
    import {
        getSellerList,sellerActivityGoodsList
    } from "@/api/base/goods/seller/goods";
    export default {
        props: {
            ptOrMg:{
                type:String,
                default: "",
            },
            acId:{
                type:String,
                default: "",
            }
        },
        data(){
            return{
                selData:[],
                loading:false,
                isShow: false,
                data:[],
                page: {
                    pageSize: 10,
                    currentPage: 1,
                    total: 0
                },
                option: {
                    menu: false,
                    header:false,
                    size: 'mini',
                    index:false,
                    tip: false,
                    border: true,
                    index: false,
                    selection: true,
                    delBtn:false,
                    viewBtn: false,
                    editBtn: false,
                    addBtn: false,
                    rowKey: 'id',
                    height: 'auto',
                    // calcHeight: 380,
                    dialogType: 'drawer',
                    align: 'center',
                    menuWidth: 150,
                    column: [
                        {
                            label: "创建时间",
                            prop: "addTime",
                            fixed: true,
                            width:200,
                            rules: [{
                                required: true,
                                message: "请输入创建时间",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: "商品名称",
                            prop: "goodsName",
                            fixed: true,
                            rules: [{
                                required: true,
                                message: "请输入评论主体id",
                                trigger: "blur"
                            }]
                        },
                        {
                            label: "主图片",
                            prop: "goodsMainPhotoUrl",
                            slot: true,
                            width:200,
                            fixed: true,
                            rules: [{
                                required: true,
                                message: "请输入评价用户id",
                                trigger: "blur"
                            }]
                        },
                    ]
                },
            }
        },
        created() {
            this.onLoad(this.page)
            this.selData=[]
        },
        methods:{
            //点击确定按钮
            sureBtn(){
                if(this.selData.length>0){
                    if(this.ptOrMg=='hd'){
                        if(this.selData.length>4){
                            this.$message({
                                message: '选择得商品不得大于4',
                                type: 'error'
                            });
                        }else{
                            this.$emit("changeGoods",this.selData);
                            this.isShow = false;
                            this.selData=[]
                        }
                    }else{
                        this.$emit("changeGoods",this.selData);
                        this.isShow = false;
                        this.selData=[]
                    }
                }else{
                    this.$message({
                        message: '请选择要添加的商品',
                        type: 'error'
                    });

                }
            },
            handleToggleIsShow(flag) {
                this.isShow = flag;
            },
            handleClose() {
                //清空
                this.selData=[]
                this.$emit('func',false)
                this.isShow = false;
            },
            sizeChange(val){
                this.page.pageSize=val
                this.onLoad(this.page)
            },
            currentChange(val){
                this.page.currentPage=val
                this.onLoad(this.page)
            },
            selectionChange(val){
                //拼接数据
				this.selData = []
                this.selData=this.selData.concat(val)
            },
            //获取商品
            onLoad (page,params = {}) {
                console.log(this.ptOrMg)
                this.loading = true;
                //判断平台还是卖家还是活动
                if(this.ptOrMg=='pt'){
                    getList(page.currentPage, page.pageSize).then(res => {
                        if(res.data.code==200){
                            this.loading = false;
                            this.data=res.data.data;
                            this.page.total=res.data.count
                        }
                    });
                }else if(this.ptOrMg=='mg'){
                   params.goodsStatus=0
                    getSellerList(page.currentPage, page.pageSize,params).then(res=>{
                        if(res.data.code==200){
                            this.loading = false;
                            this.data=res.data.data;
                            this.page.total=res.data.count
                        }
                    })

                }else if(this.ptOrMg=='hd'){
                    console.log("活动",this.acId)
                    params.acId=this.acId
                    sellerActivityGoodsList(page.currentPage, page.pageSize,params).then(res=>{
                        console.log(res)
                        if(res.data.code==200){
                            this.loading = false;
                            this.data=res.data.data.data;
                            this.page.total=res.data.data.count
                        }
                    })

                }
            },
        },
    }
</script>
<style lang="scss" scoped>
    .btn{
        div{
            width: 155px;
            margin: 0px auto;
        }

    }
</style>